from usr.window import Window
from usr.theme import Theme
import usr.store as store
import utime

WEEK_NAMES = ["一", "二", "三", "四", "五", "六", "日"]


class MainWindow(Window):
    def on_create(self):
        self.__init()

    def __init(self):
        self.__temp = ''
        self.__humi = ''
        self.__battery = ''
        self.__date = ''
        self.__week = ''
        self.__time = ''
        self.__first = True
        self.visible = False

    def on_show(self):
        self.__first = True

    def on_close(self):
        self.__init()

    def on_draw(self):
        if not self.visible:
            return
        if self.__first:
            self.__first = False
            self.draw_background()
            self.draw_statusbar()
            self.draw_static()
        self.show_time()
        self.show_data()

    def draw_static(self):
        self.canvas.draw_icon(207, 5, '4gnet', 24, 16, Theme.THEME_STATUSBAR_FG, Theme.THEME_STATUSBAR_BG)
        #self.canvas.draw_line(0, 29, 239, 29, Theme.THEME_BORDER)
        #self.canvas.draw_line(0, 118, 239, 118, Theme.THEME_BORDER)
        self.canvas.draw_text(20, 46, "    年  月  日", Theme.THEME_FOREGROUND, Theme.THEME_BACKGROUND)
        self.canvas.draw_text(85, 86, "星期", Theme.THEME_WEEK, Theme.THEME_BACKGROUND)
        # 温湿度
        self.canvas.draw_text(16, 135, "温度:      ℃", Theme.THEME_FOREGROUND, Theme.THEME_BACKGROUND)
        self.canvas.draw_text(16, 167, "湿度:      ％", Theme.THEME_FOREGROUND, Theme.THEME_BACKGROUND)
        self.canvas.draw_text(16, 199, "电量:      V", Theme.THEME_FOREGROUND, Theme.THEME_BACKGROUND)


    def show_time(self):
        time = utime.localtime()
        # yyyy年mm月dd日
        year_str = "{0:04d}".format(time[0])
        mouth_str = "{0:02d}".format(time[1])
        day_str = "{0:02d}".format(time[2])
        date_str = "{0:04d}-{1:02d}-{2:02d}".format(time[0], time[1], time[2])
        if self.__date != date_str:
            self.__date = date_str
            self.canvas.draw_ascii_text(20, 46, 16, 24, year_str, Theme.THEME_FOREGROUND, Theme.THEME_BACKGROUND)
            self.canvas.draw_ascii_text(108, 46, 16, 24, mouth_str, Theme.THEME_FOREGROUND, Theme.THEME_BACKGROUND)
            self.canvas.draw_ascii_text(164, 46, 16, 24, day_str, Theme.THEME_FOREGROUND, Theme.THEME_BACKGROUND)
        week_str = WEEK_NAMES[time[6]]
        if self.__week != week_str:
            self.__week = week_str
            self.canvas.draw_chinese_text(133, 86, 24, 24, week_str, Theme.THEME_WEEK, Theme.THEME_BACKGROUND)
        time_str = "{0:02d}:{1:02d}".format(time[3], time[4], time[5])
        if self.__time != time_str:
            self.__time = time_str
            #self.canvas.draw_ascii_text(140, 78, 16, 24, time_str, Theme.THEME_FOREGROUND, Theme.THEME_BACKGROUND)
            self.canvas.draw_ascii_text(8, 5, 8, 16, time_str, Theme.THEME_STATUSBAR_FG, Theme.THEME_STATUSBAR_BG)

    def show_data(self):
        if self.__temp != store.temp:
            self.__temp = store.temp
            self.canvas.draw_ascii_text(
                96, 135, 16, 24, self.__temp, Theme.THEME_TEMP, Theme.THEME_BACKGROUND)

        if self.__humi != store.humi:
            self.__humi = store.humi
            self.canvas.draw_ascii_text(
                96, 167, 16, 24, self.__humi, Theme.THEME_HUMI, Theme.THEME_BACKGROUND)
        if self.__battery != store.battery:
            self.__battery = store.battery
            self.canvas.draw_ascii_text(
                96, 199, 16, 24, self.__battery, Theme.THEME_LIGHT, Theme.THEME_BACKGROUND)
